package com.konne.nightmare.FastPublicOpinion.ui.information.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.collection.ArraySet;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.konne.nightmare.FastPublicOpinion.R;
import com.konne.nightmare.FastPublicOpinion.base.BaseMvpFragment;
import com.konne.nightmare.FastPublicOpinion.mvp.presenter.MoveGroupPresenter;
import com.konne.nightmare.FastPublicOpinion.mvp.view.IMoveGroupView;
import com.konne.nightmare.FastPublicOpinion.utils.LoadProgressDialog;

import java.util.List;
import java.util.Set;

import butterknife.BindView;

/**
 * Create by ws.
 * on Data:2022/1/26  Time:上午 10:20.
 * description: ** .
 */
public class MoveGroupFragment extends BaseMvpFragment<IMoveGroupView, MoveGroupPresenter> implements IMoveGroupView, View.OnClickListener {
    @BindView(R.id.recyclerView)
     RecyclerView recyclerView;
    public static MoveGroupFragment getInstance(){
        return new MoveGroupFragment();
    }

    @Override
    protected MoveGroupPresenter createPresenter() {
        return new MoveGroupPresenter();
    }

    @Override
    public LoadProgressDialog getLoadDialog() {
        return null;
    }

    @Override
    public void cancelLoadDialog() {

    }

    @Override
    public int getLayout() {
        return R.layout.frag_move_group;
    }

    @Override
    public void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        findViewById(R.id.stv_cancel).setOnClickListener(this::onClick);
        findViewById(R.id.stv_submit).setOnClickListener(this::onClick);

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        mPresenter.init();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.stv_cancel:
                getActivity().finish();
                break;
            case R.id.stv_submit:
                mPresenter.submit();
                break;
        }
    }

    @Override
    public void endSubmit() {
        getActivity().finish();
    }

    @Override
    public void updataGroupData(List<MoveGroupPresenter.GroupBean> datas) {
        recyclerView.setAdapter(new MoveGroupAdapter(datas));
    }
    class MoveGroupAdapter extends BaseQuickAdapter<MoveGroupPresenter.GroupBean, BaseViewHolder>{
        private MoveGroupPresenter.GroupBean selGroupBean;
        public MoveGroupAdapter(@Nullable List<MoveGroupPresenter.GroupBean> data) {
            super(R.layout.item_move_group,data);
        }

        @Override
        protected void convert(BaseViewHolder helper, MoveGroupPresenter.GroupBean item) {
            helper.setText(R.id.tv_title,item.title)
                    .setText(R.id.stv_sub_title,item.studentNum+"人")
                    .setImageResource(R.id.iv_sel,selGroupBean==item?R.drawable.login_xz:R.drawable.login_wxz);
            helper.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(selGroupBean==item){
                        selGroupBean=null;
                    }else {
                        selGroupBean=item;
                    }
                    notifyDataSetChanged();
                }
            });
        }
    }
}
